/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: AbstractTreeNode.java
 * (C) Copyright Foreveross Corporation 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.common.structure.impl;

import org.foliage.common.structure.TreeNode;

/**
 * 一般有序树结点数据结构对象抽象类。
 * 
 * @author liuzheng@foreveross.com
 * @version 1.0.0
 */
public abstract class AbstractTreeNode implements TreeNode {

	// ----------------------------------------------- ATTRIBUTE

	/** 父结点实例。 */
	AbstractTreeNode parent;
	/** 子结点实例数组。 */
	AbstractTreeNode[] children;

	// ----------------------------------------------- CONSTRUCTOR

	public AbstractTreeNode() {}

	// ----------------------------------------------- METHOD

	public boolean hasChild() {
		return children != null && children.length != 0;
	}

	public AbstractTreeNode getChild(String key) {
		if (key != null && hasChild()) {
			for (AbstractTreeNode child : getChildren()) {
				if (key.equals(child.getKey())) return child;
			}
		}
		return null;
	}

	public AbstractTreeNode getChild(int index) {
		if (hasChild() && index < children.length) {
			return children[index];
		}
		return null;
	}

	// ----------------------------------------------- GETTER&&SETTER

	public AbstractTreeNode getParent() {
		return parent;
	}

	public void setParent(AbstractTreeNode parent) {
		this.parent = parent;
	}

	public AbstractTreeNode[] getChildren() {
		return children;
	}

	public void setChildren(AbstractTreeNode[] children) {
		this.children = children;
	}

}
